﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace MLP
{
    class Utils
    {
        public static void Shuffle(string inputFile, string outputFile="")
        {

            string[] S1 = File.ReadAllLines(inputFile);
            string[] S2 = new string[S1.Length];
            string[] S3 = new string[S1.Length];
            S2[0] = S1[0]; //headers               
            Random R = new Random();
            int i = 1, j = 1;
            while (i < 10 * S1.Length && j < S1.Length)
            {
                int r = R.Next(1, S1.Length);
                if (S1[r] != "")
                {
                    S2[j++] = S1[r];
                    S1[r] = "";
                }
                i++;
            }
           
            i = 1;

            while (i < S1.Length)
            {
                if (S1[i] != "")
                    S2[j++] = S1[i];
                i++;
            }

            if (outputFile == "")
            {
                string fileName2 = System.IO.Path.GetFileName(inputFile);
                string fileName3 = fileName2.Substring(0, fileName2.LastIndexOf("."));
                string directoryName = System.IO.Path.GetDirectoryName(inputFile);
                File.WriteAllLines(directoryName + "\\" + fileName3 + "_sh.txt", S2);
            }
            else
                File.WriteAllLines(outputFile, S2);
        }

        public static void Tanh(string fileName, int tanhY) //tanhY: 0 classX, 1 regrX, 2 regrXY            
        {
            //string [] Headers;
            string headerLine;
            double[,] DataSet = LoadFile(fileName, out headerLine);

            int y = tanhY==2 ? 0 : 1; 
            
                for (int i = 0; i < DataSet.GetLength(0); i++)
                    for (int j = 0; j < DataSet.GetLength(1)-y; j++)
                        DataSet[i, j] = Math.Tanh(DataSet[i, j]);
            

            string fileName2 = System.IO.Path.GetFileName(fileName);
            string fileName3 = fileName2.Substring(0, fileName2.LastIndexOf("."));
            string directoryName = System.IO.Path.GetDirectoryName(fileName);
            string fn = directoryName + "\\" + fileName3 + "_tanh.txt";
            if (tanhY==2)
                fn = directoryName + "\\" + fileName3 + "_tanhY.txt";

            StreamWriter sw = new StreamWriter(fn);
            //for (int j = 0; j < DataSet.GetLength(1)-1; j++)
            //    sw.Write(Headers[j]+" ");
            //sw.WriteLine(Headers[DataSet.GetLength(1) - 1]);            
            sw.WriteLine(headerLine); 

            for (int i = 0; i < DataSet.GetLength(0)-1; i++)
            {
                for (int j = 0; j < DataSet.GetLength(1) - 1; j++)
                    sw.Write(String.Format("{0:0.00000}", DataSet[i, j]) + " ");
                if (tanhY>0)
                    sw.WriteLine(String.Format("{0:0.00000}", DataSet[i, DataSet.GetLength(1) - 1]));
                else
                    sw.WriteLine(String.Format("{0:0}", DataSet[i, DataSet.GetLength(1) - 1]));
            }
            for (int j = 0; j < DataSet.GetLength(1) - 1; j++)
                sw.Write(String.Format("{0:0.00000}", DataSet[DataSet.GetLength(0) - 1, j]) + " ");
            if (tanhY>0)
                sw.Write(String.Format("{0:0.00000}", DataSet[DataSet.GetLength(0) - 1, DataSet.GetLength(1) - 1]));
            else
                sw.Write(String.Format("{0:0}", DataSet[DataSet.GetLength(0) - 1, DataSet.GetLength(1) - 1]));

            sw.Close();

        }


        public static double[,] LoadFile(string fileName, out string headerLine)
        {
            
            StreamReader sr = new StreamReader(fileName);

            string theLine;             
           
            headerLine = sr.ReadLine();
            string[] Headers = headerLine.Split(new string[] { " ", ";" }, StringSplitOptions.RemoveEmptyEntries);
            int numAttributes = Headers.Length;
            int numVectors = 0;
            while ((theLine = sr.ReadLine()) != null)
                if (theLine.Trim().Length > 2)
                    numVectors++;
            sr.Close();
            double[,] DataSet = new double[numVectors, numAttributes];

            int v = 0;
            sr = new StreamReader(fileName);
            theLine = sr.ReadLine();
            while ((theLine = sr.ReadLine()) != null)
            {
                if (theLine.Trim().Length > 2)
                {
                    string[] S = theLine.Split(new string[] { " ", ";" }, StringSplitOptions.RemoveEmptyEntries);
                    for (int a = 0; a < numAttributes; a++)
                      //  DataSet[v, a] = Convert.ToDouble(S[a]);
                        DataSet[v, a] = Double.Parse(S[a], System.Globalization.CultureInfo.InvariantCulture);
                    v++;
                }
            }
            sr.Close();
            return DataSet;
        }



    }



}
